package edu.chinasoft.spring02;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//根据目标类把代理类创建出来
public
class CompanyFactory implements InvocationHandler {
    private Account target;
    //表示目标类（基本类）
    private XiaoWu xiaoWu;

    public
    CompanyFactory() {
    }

    public
    CompanyFactory(Account target, XiaoWu xiaoWu) {
        this.target = target;
        this.xiaoWu = xiaoWu;
    }
    //根据目标类（基本类）创建出代理类
    public
    Account createProxy() {
        //第三个参数的作用是:设置如何扩展基本类的方法
        Account proxy = (Account) Proxy.newProxyInstance( target.getClass().getClassLoader(), target.getClass().getInterfaces(),this );
        return proxy;
    }

    @Override
    public
    Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        xiaoWu.start();
        Object result = method.invoke( target, args );
        xiaoWu.end();
        return result;
    }
}
